/**
 * Created by leo on 2/2.
 */
angular.module('panels')
    .directive('panelBomActiveAlarmList', function ($http, loadConf, tabService, $timeout, $log, Promise, LoginInfo) {
        return {
            scope: {},
            link: function (scope, elem) {
                scope.eventClasses = [{id: 1, name: '严重告警'}, {id: 2, name: '重要告警'}, {id: 3, name: '一般告警'},
                    {id: 4, name: '一级预警'}, {id: 5, name: '二级预警'}];
                scope.eventClassMap = {};
                angular.forEach(scope.eventClasses, function (clazz) {
                    scope.eventClassMap[clazz.id] = clazz.name;
                });
                scope.eventCounts = {};
                scope.eventColors = {1: '#DD4444', 2: '#FF7F50', 3: '#0078E7'};
                scope.eventList = [];
                Promise.all({
                    loginInfo: LoginInfo,
                    setting: loadConf('panel-bom-active-alarm-list.yml')
                }).then(function (result) {
                    var setting = result.setting, loginInfo = result.loginInfo;
                    scope.more = setting.more;
                    var timeoutId;
                    scope.$on('$destroy', function () {
                        timeoutId && $timeout.cancel(timeoutId);
                    });
                    (function updateData() {
                        Promise.all([
                            $http.jsonp(setting.listQueryUrl.replace(/\{USER_ID}/g, loginInfo.currentUser.LOGIN_ID)).then(function (resp) {
                                scope.eventList = resp.data.rows;
                            }),
                            $http.jsonp(setting.countQueryUrl.replace(/\{USER_ID}/g, loginInfo.currentUser.LOGIN_ID)).then(function (resp) {
                                scope.eventCounts = resp.data;
                            })
                        ]).then(callback, callback);
                        function callback() {
                            // 统计和列表都请求完了之后准备下一次数据更新
                            if (setting.updateInterval > 0) {
                                timeoutId = $timeout(updateData, setting.updateInterval * 1000);
                            }
                        }

                        $log.debug('panel update data: bom-active-alarm-list');
                    }());
                    scope.openAlarmDetail = function (event) {
                        tabService.addTab({
                            name: '告警：' + event.EVENT_TITLE,
                            url: setting.viewDetailUrl.replace('{EVENT_ID}', event.EVENT_ID)
                        });
                    };
                    scope.openAlarmList = function (eventClass) {
                        tabService.addTab({
                            name: eventClass.name,
                            url: setting.viewUrl.replace('{EVENT_CLASS}', eventClass.id)
                        });
                    };
                });
            },
            //template:'<div>Hi everyone!</div>'
            templateUrl: 'app/panel/bom/active-alarm-list.html'
        };
    });